﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class news_Default2 : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string strPage = Request["page"];
            int page = 1;
            try
            {
                page = int.Parse(strPage);
                if (page < 1)
                {
                    page = 1;
                }
            }
            catch (Exception ex)
            {
                page = 1;
            }
            CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
            ddlNewType1.DataSource = dal.NewsType.Where(x => string.IsNullOrEmpty(x.pid) && x.enable == 1);
            ddlNewType1.DataTextField = "name";
            ddlNewType1.DataValueField = "id";
            ddlNewType1.DataBind();
            ddlNewType1.Items.Insert(0, new ListItem("选择", ""));
            bd(page);
        }
    }

    public int GetPage()
    {
        return AspNetPager1.CurrentPageIndex;
    }

    private void bd(int page)
    {

        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        var data = dal.News.AsQueryable();
        var newTypeId = Server.UrlDecode(Request["newsTypeId"]);
        if (newTypeId == "1002")
        {
            data = data.Where(x => !string.IsNullOrEmpty(x.Stock));
        }
        var news = data.Select(x => new { x.id, x.title, x.ctime, x.ptime, x.Stock, x.IsEnable, x.step, x.Source,x.Nature });
        var title = Server.UrlDecode(Request["title"]);
        DateTime date;
        DateTime.TryParse(Server.UrlDecode(Request["date"]), out date);
        DateTime edate;
        DateTime.TryParse(Server.UrlDecode(Request["edate"]), out edate);
        var stock = Server.UrlDecode(Request["stock"]);
        if (!string.IsNullOrEmpty(title))
            news = news.Where(x => x.title.Contains(title));
        if (date != DateTime.MinValue)
            news = news.Where(x => x.ptime == date);
        if (edate != DateTime.MinValue)
            news = news.Where(x => x.ptime == edate);
        if (date != DateTime.MinValue && edate != DateTime.MinValue)
            news = news.Where(x => x.ptime >= date && x.ptime <= edate);
        if (!string.IsNullOrEmpty(stock))
            news = news.Where(x => x.Stock.Contains(stock));
    
        if (!string.IsNullOrEmpty(newTypeId) && newTypeId.ToLower() != "null")
        {
            news = from c in dal.News_Type
                   join p in news on c.NewsId equals p.id
                   where c.NewsTypeId == newTypeId
                   select p;
        }
        AspNetPager1.RecordCount = news.Count();
        AspNetPager1.CurrentPageIndex = page;
        news = news.OrderByDescending(x => x.ctime).Skip((page - 1) * AspNetPager1.PageSize).Take(AspNetPager1.PageSize);
        Repeater1.DataSource = news;
        Repeater1.DataBind();
    }
    protected void btnPub_Click(object sender, EventArgs e)
    {
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        var list = hidIds.Value.Split(',');
        if (list.Length > 0 && list[0] != "")
        {
            for (int i = 0; i < list.Length; i++)
            {
                int id = Convert.ToInt32(list[i]);
                CMSModel.News news = dal.News.First(x => x.id == id);
                news.ptime = DateTime.Now;
                news.IsEnable = true;
                CacheClearBLL.NewsClear(id);
            }
            dal.SaveChanges();
            bd(AspNetPager1.CurrentPageIndex);
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "pub", "alert('没有选中发布项');", true);
        }
    }

    public static string GetNewsUrl(object obj)
    {
        long newsId = (long)obj.GetType().GetProperty("id").GetValue(obj, null);
        return string.Format(System.Configuration.ConfigurationManager.AppSettings["NewsUrl"], newsId);

    }

    protected void btnCancelPub_Click(object sender, EventArgs e)
    {
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        var list = hidIds.Value.Split(',');
        if (list.Length > 0 && list[0] != "")
        {
            for (int i = 0; i < list.Length; i++)
            {
                long id = Convert.ToInt64(list[i]);
                CMSModel.News news = dal.News.First(x => x.id == id);
                news.ptime = news.ctime;
                news.IsEnable = false;
                CacheClearBLL.NewsClear(id);
            }
            dal.SaveChanges();
            bd(AspNetPager1.CurrentPageIndex);
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "pub", "alert('没有选中取消发布项');", true);
        }
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        bd(AspNetPager1.CurrentPageIndex);
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        var list = hidIds.Value.Split(',');
        if (list.Length > 0 && list[0] != "")
        {
            for (int i = 0; i < list.Length; i++)
            {
                int id = Convert.ToInt32(list[i]);
                CMSModel.News news = dal.News.First(x => x.id == id);
                dal.DeleteObject(news);
            }
            dal.SaveChanges();
            bd(AspNetPager1.CurrentPageIndex);
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "pub", "alert('没有选中删除项');", true);
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string newsTypeId = Request["newsTypeId"];
        Response.Redirect("add.aspx?newsTypeId=" + newsTypeId + "&page=" + Request["page"]);
    }
}